//
//  QJCellConfig.m
//  QJWork
//
//  Created by qijia on 15/6/1.
//  Copyright (c) 2015年 高中雷. All rights reserved.
//

#import "QJCellConfig.h"
#import "QJBaseTableViewCell.h"

#import "UITableView+FDTemplateLayoutCell.h"
@implementation QJCellConfig


//只用在名片夹首页
+(NSMutableArray *)sectionAandZArray:(NSMutableArray *)ary className:(NSString *)className{
    NSMutableArray * dataArray = [[NSMutableArray  alloc]init];
    
    for(int i = 0;i<ary.count;i++){
        NSMutableArray * array = [[NSMutableArray alloc]init];
        
         [[ary objectAtIndex:i] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
             if(idx!=0){
                 id m = [self cellConfigWithClassName:className withData:obj];
                 if (m) {
                     [array addObject:m];
                 }

             }
         }];
        
//        for(id record in [ary objectAtIndex:i]){
//                   }
        [dataArray addObject:array];
    }
    
    return dataArray;

}


//带section
+(NSMutableArray *)sectionFromDataArray:(NSMutableArray *)ary className:(NSString *)className{
    
    NSMutableArray * dataArray = [[NSMutableArray  alloc]init];
    [ary enumerateObjectsUsingBlock:^(id obj, NSUInteger i, BOOL *stop) {
        NSMutableArray * array = [[NSMutableArray alloc]init];
        [[ary objectAtIndex:i] enumerateObjectsUsingBlock:^(id record, NSUInteger i, BOOL *stop) {
            id m = [self cellConfigWithClassName:className withData:record];
            if (m) {
                [array addObject:m];
            }
            }];
            [dataArray addObject:array];
    }];
    return dataArray;
}


+(NSMutableArray*)arrayFromDataArray:(NSMutableArray*)ary  className:(NSString *)className{
    NSMutableArray *aryNew = [NSMutableArray arrayWithCapacity:[ary count]];
    
    [ary enumerateObjectsUsingBlock:^(id record, NSUInteger idx, BOOL *stop) {
        id m = [self cellFromData:record className:className];
        
        if (m) {
            [aryNew addObject:m];
        }
    }];

    return aryNew;
}


////处理modelview
//+(NSMutableArray *)arrayFromViewModelDataArray:(NSMutableArray *)ary className:(NSString*)className{
//    NSMutableArray *aryNew = [NSMutableArray arrayWithCapacity:[ary count]];
//    if([className hasPrefix:@"VM_"]){
//        [ary enumerateObjectsUsingBlock:^(id record, NSUInteger idx, BOOL *stop) {
//            QJViewModel * c = [[NSClassFromString(className) alloc]init];
//            [c LoadViewFromObject:record];
//            id m = [self cellFromData:c className:[className stringByReplacingOccurrencesOfString:@"VM_" withString:@""]];
//            if (m) {
//                [aryNew addObject:m];
//            }
//        }];
//
//    }else{
//        NSLog(@"前缀没有加VM_");
//    }
//    return aryNew;
//}


+(id)cellFromData:(id)data className:(NSString *)className {
     NSMutableArray * array = [[NSMutableArray alloc]init];
    [array addObject:[self cellConfigWithClassName:className withData:data]];
    return array;
}

+(id)cellFromDataOnlyConfig:(id)data className:(NSString *)className {
    return [self cellConfigWithClassName:className withData:data];
}

+ (instancetype)cellConfigWithClassName:(NSString *)className withData:(id)data
{
    QJCellConfig *cellConfig = [QJCellConfig new];
    cellConfig.className = className;
    cellConfig.data = data;
    return cellConfig;
}

- (UITableViewCell *)cellOfCellConfigWithTableView:(UITableView *)tableView IndexPath:(NSIndexPath *)indexPath  withContainer:(id<QJCellControlClientDelegate>)containerDelegate{
    // 重用cell
     NSString *cellID = self.className;
      [tableView registerClass:NSClassFromString(cellID) forCellReuseIdentifier:cellID];
     QJBaseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
//     QJBaseTableViewCell * cell = (QJBaseTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
    cell.delegate = containerDelegate;
    [cell LoadViewFromObject:self.data];
    return cell;
}


//// If you are not using auto layout, override this method
//- (CGSize)sizeThatFits:(CGSize)size
//{
//    CGFloat totalHeight = 0;
//    totalHeight += [self.titleLabel sizeThatFits:size].height;
//    totalHeight += [self.contentLabel sizeThatFits:size].height;
//    totalHeight += [self.contentImageView sizeThatFits:size].height;
//    totalHeight += [self.usernameLabel sizeThatFits:size].height;
//    totalHeight += 40; // margins
//    return CGSizeMake(size.width, totalHeight);
//}
-(CGFloat)cellHeightWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    NSString *cellID = self.className;
    [tableView registerClass:NSClassFromString(cellID) forCellReuseIdentifier:cellID];
      return [tableView fd_heightForCellWithIdentifier:self.className configuration:^(QJBaseTableViewCell *cell) {
          [cell LoadViewFromObject:self.data];
    }];
}


-(CGFloat)fd_cellHeightWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    NSString *cellID = self.className;
    [tableView registerClass:NSClassFromString(cellID) forCellReuseIdentifier:cellID];
    return [tableView fd_heightForCellWithIdentifier:self.className cacheByIndexPath:indexPath configuration:^(QJBaseTableViewCell *cell) {
        [cell LoadViewFromObject:self.data];
    }];
}

















@end
